<template>
  <section class="app-inner">
    <el-card class="header-card">
      <el-form :inline="true" :model="form" size="small">
        <el-form-item label="部门名称">
          <el-input clearable placeholder="请输入部门名称" v-model="form.key"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit" style="margin-left: 30px">查询</el-button>
          <el-button type="primary" @click="handleSynchro">刷新</el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <el-card class="table-card">
      <section class="table-btn">
        <el-button @click="handleAdd" type="primary" icon="plus" size="small">添加部门</el-button>
        <!-- 该功能无设计, 先隐藏(2021.7.15) -->
        <!-- <el-button
          style="margin-left: 8px"
          @click="handleMove"
          type="primary"
          icon="swap"
          size="small"
          >部门迁移</el-button
        > -->
      </section>

      <el-table
        border
        :data="tableData"
        style="width: 100%"
        row-key="pkId"
        :tree-props="{ children: 'children' }"
      >
        <el-table-column
          align="left"
          header-align="left"
          prop="deptName"
          label="部门名称"
        ></el-table-column>
        <el-table-column prop="deptLevel" label="部门级别">
          <template slot-scope="scope">
            <div>{{ getLabel(1, scope.row.deptLevel) }}</div>
          </template>
        </el-table-column>
        <el-table-column prop="deptHead" label="主管人员"></el-table-column>
        <el-table-column prop="headMobile" label="主管电话"></el-table-column>
        <el-table-column prop="isEnable" label="是否启用">
          <template slot-scope="scope">
            <div>{{ getLabel(2, scope.row.isEnable) }}</div>
          </template>
        </el-table-column>
        <el-table-column prop="pdeptName" label="上级部门"></el-table-column>
        <el-table-column prop="creater" label="创建人"></el-table-column>
        <el-table-column prop="createTime" label="创建时间"></el-table-column>
        <el-table-column prop="remark" label="备注"></el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button type="text" size="small" @click="handleEdit(scope.row)">编辑</el-button>
            <el-button type="text" size="small" @click="handleDelete(scope.row.deptId)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <department-modal ref="DepartmentModal" @ok="DepartmentModalOk"></department-modal>

    <department-move-modal ref="DepartmentMoveModal" @ok="DepartmentModalOk"></department-move-modal>
  </section>
</template>

<script>
import { deptLevel, accountIsEnable } from './js/constants'
import DepartmentModal from './components/DepartmentModal'
import DepartmentMoveModal from './components/DepartmentMoveModal'
import { getDepartmentTree, deleteDepartment } from './js/auth'

export default {
  name: 'DepartmentList',
  components: { DepartmentModal, DepartmentMoveModal },
  data () {
    return {
      form: {
        key: ''
      },
      tableData: []
    }
  },
  created () {
    let _this = this
    this.$nextTick(() => {
      _this.getDepartmentTree1()
    })
    // this.getDepartmentTree1()
  },
  methods: {
    // 查询按钮
    onSubmit () {
      this.getDepartmentTree1()
    },

    // 获取部门列表
    async getDepartmentTree1 () {
      const { code, data } = await getDepartmentTree(this.form)
      if (code === 0) {
        this.tableData = data === null ? [] : data
      }
    },

    // 部门新增
    handleAdd () {
      this.$refs.DepartmentModal.add()
    },

    // 编辑按钮
    handleEdit (row) {
      this.$refs.DepartmentModal.edit(row)
    },

    // 删除按钮
    handleDelete (deptId) {
      this.$confirm('此操作将永久删除该条记录, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          this.dataDelete(deptId)
        })
        .catch(() => {
          this.$message.warning('已取消删除')
        })
    },
    async dataDelete (deptId) {
      const { code } = await deleteDepartment(deptId)
      if (code === 0) {
        this.$message.success('删除成功')
        this.getDepartmentTree1()
      }
    },

    // 部门迁移
    handleMove () {
      this.$refs.DepartmentMoveModal.edit()
    },

    // 弹框回调
    DepartmentModalOk () {
      this.getDepartmentTree1()
    },

    // 刷新按钮
    handleSynchro () {
      this.$confirm('将手动同步所有部门信息，是否继续执行？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        this.getDepartmentTree1()
      })
    },

    // 获取label
    getLabel (type, val) {
      if (type === 1) {
        return this.$base.constFormatter(deptLevel, val)
      }
      if (type === 2) {
        return this.$base.constFormatter(accountIsEnable, val)
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.app-inner {
  .table-btn {
    margin-bottom: 15px;
  }
  /deep/ .el-table .is-left {
    text-align: left !important;
    padding-left: 15px;
  }
}
</style>
